<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="线索人名称" prop="intendedName">
      <el-input v-model="dataForm.intendedName" placeholder="线索人名称"></el-input>
    </el-form-item>
    <el-form-item label="线索人联系电话" prop="intendedPhone">
      <el-input v-model="dataForm.intendedPhone" placeholder="线索人联系电话"></el-input>
    </el-form-item>
      <el-form-item label="线索人性别" prop="intendedSex">
        <el-select v-model="dataForm.intendedSex" placeholder="请选择">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="线索人年龄" prop="intendedAge">
      <el-input v-model="dataForm.intendedAge" placeholder="线索人年龄"></el-input>
    </el-form-item>
      <el-form-item label="信息来源" prop="intendedFrom">
        <el-select v-model="dataForm.intendedFrom" placeholder="请选择">
          <el-option label="App" value="App"></el-option>
          <el-option label="网站" value="网站"></el-option>
          <el-option label="系统" value="系统"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="推荐渠道" prop="intendedBranch">
        <el-select v-model="dataForm.intendedBranch" placeholder="推荐渠道">
          <el-option label="公众号推广" value="公众号推广"></el-option>
          <el-option label="朋友推荐" value="朋友推荐"></el-option>
        </el-select>
      </el-form-item>
<!--    <el-form-item label="归属营销人员编号" prop="intendedOwner">-->
<!--      <el-input v-model="dataForm.intendedOwner" placeholder="归属营销人员编号"></el-input>-->
<!--    </el-form-item>-->
      <el-form-item label="归属营销人员编号" prop="postId">
        <el-select v-model="dataForm.intendedOwner" placeholder="请选择">
          <el-option v-for="(item,i) in employeeModel" :label="item.employeeNameConcatId" :value="item.employeeId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="意向状态" prop="intendedStatus">
        <el-select v-model="dataForm.intendedStatus" placeholder="意向状态">
          <el-option label="一般" value="一般"></el-option>
          <el-option label="强烈" value="强烈"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    created () {
      this.getEmployeeInfo()
    },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          intendedName: '',
          intendedPhone: '',
          intendedSex: '',
          intendedAge: '',
          intendedFrom: '',
          intendedBranch: '',
          intendedOwner: '',
          intendedStatus: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          intendedName: [
            { required: true, message: '线索人名称不能为空', trigger: 'blur' }
          ],
          intendedPhone: [
            { required: true, message: '线索人联系电话不能为空', trigger: 'blur' }
          ],
          intendedSex: [
            { required: true, message: '线索人性别不能为空', trigger: 'blur' }
          ],
          intendedAge: [
            { required: true, message: '线索人年龄不能为空', trigger: 'blur' }
          ],
          intendedFrom: [
            { required: true, message: '信息来源不能为空', trigger: 'blur' }
          ],
          intendedBranch: [
            { required: true, message: '推荐渠道不能为空', trigger: 'blur' }
          ],
          intendedOwner: [
            { required: true, message: '归属营销人员不能为空', trigger: 'blur' }
          ],
          intendedStatus: [
            { required: true, message: '意向状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        },
        employeeModel: {
          employeeId: '',
          employeeNameConcatId: ''
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_intended_customers/bsintendedcustomers/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.intendedName = data.bsIntendedCustomers.intendedName
                this.dataForm.intendedPhone = data.bsIntendedCustomers.intendedPhone
                this.dataForm.intendedSex = data.bsIntendedCustomers.intendedSex
                this.dataForm.intendedAge = data.bsIntendedCustomers.intendedAge
                this.dataForm.intendedFrom = data.bsIntendedCustomers.intendedFrom
                this.dataForm.intendedBranch = data.bsIntendedCustomers.intendedBranch
                this.dataForm.intendedOwner = data.bsIntendedCustomers.intendedOwner
                this.dataForm.intendedStatus = data.bsIntendedCustomers.intendedStatus
                this.dataForm.createTime = data.bsIntendedCustomers.createTime
                this.dataForm.updateTime = data.bsIntendedCustomers.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_intended_customers/bsintendedcustomers/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'intendedName': this.dataForm.intendedName,
                'intendedPhone': this.dataForm.intendedPhone,
                'intendedSex': this.dataForm.intendedSex,
                'intendedAge': this.dataForm.intendedAge,
                'intendedFrom': this.dataForm.intendedFrom,
                'intendedBranch': this.dataForm.intendedBranch,
                'intendedOwner': this.dataForm.intendedOwner,
                'intendedStatus': this.dataForm.intendedStatus,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      //获取员工信息下拉
      getEmployeeInfo () {
        this.$http({
          url: this.$http.adornUrl(`/tb_bs_protocol/bsprotocol/queryAllEmployee`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.employeeModel = data.date
            console.log(this.employeeModel)
          }
        })
      }
    }
  }
</script>
